/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ez2find.manager;

import com.ez2find.model.Message;
import com.ez2find.model.Topic;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author org.@le
 */
@Stateless
public class MessageManagerBean implements MessageManagerLocal {
    
    
    @PersistenceContext
    private EntityManager em;
    
    public void create(Message message)throws Exception {
        em.persist(message);
    }
 
    public void update(Message message)throws Exception {
        em.merge(message);
    }
    
    public void delete(Message message)throws Exception {
        message=em.merge(message);
        em.remove(message);
    }
    
    public Message find(long id){
        return em.find(Message.class,id);
    }
    
    public List<Message> listAll(){
        return em.createQuery("select c from Message c").getResultList();
    }
        public List<Message> messagesTopic(Topic topic) {
        Query query = em.createNamedQuery("messagesTopic");
        query.setParameter("topic", topic);
        List<Message> result = query.getResultList();
        return result;
    }
        public List<Message> messagesMessage(Message message) {
        Query query = em.createNamedQuery("messagesMessage");
        query.setParameter("message", message);
        List<Message> result = query.getResultList();
        return result;
    }
        
 
}
